package com.cn.onetrip.activity;

import java.util.List;
import java.util.Map;

import com.cn.onetrip.adapter.TravelDetailItemAdapter;
import com.cn.onetrip.objects.ArticleListObj;
import com.cn.onetrip.objects.ArticleObj;
import com.cn.onetrip.objects.InfosListObj;
import com.cn.onetrip.objects.NotesListObj;
import com.cn.onetrip.objects.RouteObj;
import com.cn.onetrip.objects.VisitsListObj;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.ActivityTag;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.TitleScrollManager;
import com.cn.onetrip.untility.Untility;
import com.cn.onetrip.view.AdView;
import com.cn.onetrip.view.ContentScrollView;
import com.cn.onetrip.view.ScrollListView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TravelDetailActivity extends Activity implements
		OnItemClickListener {
	// public static View view;
	private ScrollListView travelList;
	private TextView textViewTitle, textViewTimeAuthor;
	private TravelDetailItemAdapter adapter;
	private Button quit;
	private int itemId, routeNum;
	private String tag, itemTitle, itemTime, itemSource;
	private boolean isLeft = false;
	private ImageView titleImageView;
	private int titleImageSrc;
	private View footImageView;
	private View headView;
	private AdView adView;
	private RelativeLayout layoutTitle;
	private TitleScrollManager titleScrollManager;
	
	private ContentScrollView scrollView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Window w = getWindow();
		// view = w.getDecorView();
		if (SysApplication.notesList.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_travel_detail);

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		titleImageSrc = bundle.getInt("title_image");
		tag = bundle.getString("module");
		isLeft = bundle.getBoolean("Left");
		itemId = bundle.getInt("itemId");
		itemTitle = bundle.getString("title");
		itemTime = bundle.getString("time");
		itemSource = bundle.getString("source");
		if (tag.equals(ActivityTag.Route)) {
			routeNum = bundle.getInt("route_num");
		}
		headView = getLayoutInflater().inflate(R.layout.activity_travel_head,
				null);
		titleImageView = (ImageView) findViewById(R.id.titleImage);
		titleImageView.setBackgroundResource(titleImageSrc);
		textViewTitle = (TextView) headView
				.findViewById(R.id.travelDetailTitle);
		textViewTimeAuthor = (TextView) headView
				.findViewById(R.id.travelDetailTimeAuthor);
		textViewTitle.setText(itemTitle);
		if (itemTime == null || itemSource == null || itemTime.equals("")
				|| itemSource.equals("")) {
			textViewTimeAuthor.setVisibility(View.GONE);
		} else {
			textViewTimeAuthor.setText(itemTime + "  "
					+ getResources().getString(R.string.travel_source)
					+ itemSource);
		}
		travelList = (ScrollListView) findViewById(R.id.travelDetailList);
		adapter = new TravelDetailItemAdapter(TravelDetailActivity.this,
				travelList, handler);
		loadDate();

		scrollView = (ContentScrollView) findViewById(R.id.scrollContent);
		
		footImageView = getLayoutInflater().inflate(R.layout.foot_view, null);
		travelList.addHeaderView(headView);
		travelList.addFooterView(footImageView);
		travelList.setAdapter(adapter);
		new Untility().setListViewHeightBasedOnChildren(travelList);
		travelList.setOnItemClickListener(this);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});

		adView = (AdView) findViewById(R.id.ad_view);
		layoutTitle = (RelativeLayout) findViewById(R.id.LayoutTopTravelDetail);
		
		ViewTreeObserver vto = travelList.getViewTreeObserver();  
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  

            @Override  
            public void onGlobalLayout() {  

            	travelList.getViewTreeObserver().removeGlobalOnLayoutListener(this);   

            	View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
				int height = travelList.getHeight();
				
				if (height > v.getHeight()+50) {
					titleScrollManager = new TitleScrollManager(layoutTitle,
							adView, scrollView, null);
				}
            }  
        });  
	}

	@Override
	protected void onResume() {
		adView.startTimer();
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		adView.stopTimer();
	}

	@Override
	protected void onDestroy() {
		adapter.clean();
		adapter.threadQuit();
		adapter.clear();
		if (titleScrollManager != null) {
			titleScrollManager.stopTimer();
			titleScrollManager = null;
		}
		
		super.onDestroy();
	}

	public void loadDate() {
		adapter.clean();
		List<ArticleObj> ArticleList = null;
		if (tag.equals(ActivityTag.Travel)) {
			List<NotesListObj> notesList = SysApplication.notesList;
			NotesListObj noteObj = notesList.get(itemId);
			ArticleList = noteObj.detailedsArticles;
		} else if (tag.equals(ActivityTag.Route)) {
			List<VisitsListObj> visitsListObj = SysApplication.visitsListObj;
			VisitsListObj visitsList = visitsListObj.get(routeNum);
			List<ArticleListObj> articlesListObj = visitsList.detailedsSites;
			ArticleListObj articleList = articlesListObj.get(itemId);
			ArticleList = articleList.detailedsArticleList;
		} else if (tag.equals(ActivityTag.Info)) {
			Map<String, List<InfosListObj>> notesList = SysApplication.infosMapList;
			if (isLeft) {
				List<InfosListObj> infosList = notesList.get("news");
				InfosListObj infosObj = infosList.get(itemId);
				ArticleList = infosObj.detailedsInfosList;
			} else {
				List<InfosListObj> infosList = notesList.get("tips");
				InfosListObj infosObj = infosList.get(itemId);
				ArticleList = infosObj.detailedsInfosList;
			}
		}

		for (int i = 0; i < ArticleList.size(); i++) {
			ArticleObj articleObj = ArticleList.get(i);
			adapter.addBooks(articleObj.title, articleObj.icon,
					articleObj.description);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	public Handler handler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			if (msg.what == 0) {
			}
		};
	};
}